package edu.spec.sms.controller;

import edu.spec.sms.model.Clazz;
import edu.spec.sms.model.ClazzDto;
import edu.spec.sms.model.Student;
import edu.spec.sms.model.questionnaire.Questionnaire;
import edu.spec.sms.service.iface.IClazzService;
import edu.spec.sms.service.iface.IQuestionnaireClazzService;
import edu.spec.sms.service.iface.IQuestionnaireService;
import edu.spec.sms.service.iface.IStudentSubjectService;
import edu.spec.sys.model.dto.MvcReturn;
import edu.spec.sys.model.enumeration.Status;
import org.madgecko.basic.model.Pager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Author       : MadFrog
 * Date         : 2018/2/18 12:30
 * Description  : " 问卷调查" 视图控制器
 */
@Controller
public class QuestionnaireController {

    @Inject
    private IQuestionnaireService questionnaireService;
    @Inject
    private IQuestionnaireClazzService questionnaireClazzService;
    @Inject
    private IClazzService clazzService;
    @Inject
    private IStudentSubjectService studentSubjectService;

    @ResponseBody
    @RequestMapping("/admin/questionnaire/add")
    public MvcReturn add(Questionnaire q){
        q.setCreateDate(new Date());
        questionnaireService.add(q);
        return new MvcReturn(true);
    }

    @ResponseBody
    @RequestMapping("/admin/questionnaire/toggleStatus/{id}")
    public MvcReturn toggleStatus(@PathVariable Integer id){
        Questionnaire q = questionnaireService.load(id);
        if(q.getStatus().equals(Status.ENABLED)){
            q.setStatus(Status.DISABLED);
        }else {
            q.setStatus(Status.ENABLED);
        }
        questionnaireService.update(q);
        return new MvcReturn(true);
    }

    @ResponseBody
    @RequestMapping("/admin/questionnaire/findByCon")
    public MvcReturn findByCon(){
        Pager<Questionnaire> p = questionnaireService.findByCon();
        return new MvcReturn(true, p.getDatas(), p.getTotal());
    }

    @ResponseBody
    @RequestMapping("/student/questionnaire/listLatestByStudent")
    public MvcReturn listLatestByStudent(HttpSession session){
        Student student = (Student)session.getAttribute("user");
        List<Questionnaire> qList = questionnaireService.listLatestByStudent(student.getId());
        return new MvcReturn(true, qList);
    }

    @ResponseBody
    @RequestMapping("/admin/questionnaire/addClazz/{examId}/{clazzId}")
    public MvcReturn addClazz(@PathVariable Integer examId, @PathVariable Integer clazzId){
        questionnaireService.addClazz(examId, clazzId);
        return new MvcReturn(true);
    }

    /**
     * 删除QuestionnaireClazz，同时也要删除QuestionnaireStudent
     * @param questionnaireId 问卷主键
     * @param clazzId 班级主键
     * @return
     */
    @ResponseBody
    @RequestMapping("/admin/questionnaire/delClazz/{questionnaireId}/{clazzId}")
    public MvcReturn delClazz(@PathVariable Integer questionnaireId, @PathVariable Integer clazzId){
        questionnaireClazzService.delete(questionnaireId, clazzId);

        return new MvcReturn(true);
    }

    @ResponseBody
    @RequestMapping("/admin/questionnaire/listClazzDto")
    public MvcReturn listClazzDto(Integer examId){
        List<Clazz> allClazz = clazzService.listInSchoolClazz();
        List<Integer> ids = questionnaireService.listClazzIdsByquestionnaire(examId);
        List<ClazzDto> clazzDtos = new ArrayList<ClazzDto>();
        for(Clazz c : allClazz){
            ClazzDto cd = new ClazzDto();
            cd.setSelected(false);
            cd.setId(c.getId());
            cd.setName(c.getName());
            cd.setStatus(c.getStatus());
            cd.setStudentNumber(c.getStudentNumber());
            for(Integer id : ids){
                if (cd.getId()==id){
                    cd.setSelected(true);
                }
            }
            clazzDtos.add(cd);
        }
        return new MvcReturn(true, clazzDtos);
    }

    /**
     * 学生提交问卷调查结果
     * @param subjectId_answer
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/student/questionnaire/finish", method = RequestMethod.POST)
    public MvcReturn finish(@RequestParam(value="subjectId_answer[]") String[] subjectId_answer, HttpSession session){
        Student s = (Student)session.getAttribute("user");
        Integer questionnaireId = (Integer)session.getAttribute("questionnaireId");
        studentSubjectService.finish(s.getId(), questionnaireId, subjectId_answer);
        return new MvcReturn(true);
    }

}
